﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using MySql.Web;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public string version = "1100";
        public string connectionstring = null;
        public Boolean Switch = false;
        public IPAddress[] myaddr = Dns.GetHostAddresses("localhost");
        public string getip(IPAddress[] addr)
        {
            string str = null;
            string result = null;
            foreach (IPAddress address in addr)
            {
                str = address.ToString();
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] >= '0' && str[i] <= '9' || str[i] == '.')
                    {
                        result = str;
                    }
                }
            }
            return result;

        }
        public Form1()
        {
            InitializeComponent();
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Aboutbutton_Click(object sender, EventArgs e)
        {
          
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ABTBOX abt = new ABTBOX();
            abt.Show();
        }

        private void EVE市场中心ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Artemis.EveCentral ECT = new Artemis.EveCentral(null,Switch);
            ECT.Show();
        }

        private void 虫洞查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Artemis.WSpaceQuery wsq = new Artemis.WSpaceQuery(Switch);
            wsq.Show();
        }

        private void Api输入ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show("敬请期待");
        }

        private void 检查连接ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection conn = new MySqlConnection(connectionstring);
                conn.Open();
                conn.Close();
                MessageBox.Show("数据库连接正常");

            }
            catch (MySqlException)
            {
                MessageBox.Show("数据库连接异常\n请检查网络连接或本工具官方公告");
            }
        }

        private void 制造计算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not Implented");
        }

        private void 物品查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Artemis.ItemQuery IQR = new Artemis.ItemQuery(Switch);
            IQR.Show();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            try
            {
                try
                {
                    connectionstring = ConfigurationManager.ConnectionStrings["MainDataBase"].ConnectionString;
                    MySqlConnection conn1 = new MySqlConnection(connectionstring);
                    conn1.Open();
                    conn1.Close();
                }
                catch (MySqlException k)
                {
                    Switch = true;
                    MessageBox.Show(k.Message);

                }
                
                if (Switch == true)
                {
                    connectionstring = ConfigurationManager.ConnectionStrings["BackupDataBase"].ConnectionString;
                    MessageBox.Show("主数据库无法连接，将使用备用数据库");
                }
                else
                {
                    connectionstring = ConfigurationManager.ConnectionStrings["MainDataBase"].ConnectionString;
                }
                MySqlConnection conn = new MySqlConnection(connectionstring);
                conn.Open();
                conn.Close();

            }
            catch (MySqlException)
            {
                MessageBox.Show("数据库连接异常\n请检查网络连接或本工具官方公告");
                Close();
            }
            finally
            {
                //string versioncontrol = "select version from versions.maintainingversions where version = @version";
                //MySqlCommand query = new MySqlCommand(versioncontrol);
                //query.Parameters.Add(new MySqlParameter("@version", version));
                //DataSet versions = Artemis.EveCentral.getdata(query, "version", connectionstring);
                //if (versions == null || versions.Tables[0].Rows.Count == 0)
                //{
                    //Close();
                //}
            }
            try
            {
                string serinity_online = null;
                string tranquility_online = null;
                try
                {
                    TcpClient serinity = new TcpClient("211.144.214.68", 26000);
                    if (serinity.Connected == true)
                    {
                        byte[] data = new byte[1024];
                        NetworkStream ns = serinity.GetStream();
                        ns.Read(data, 0, 1024);
                        string content = (data[21] * 256 + data[20]).ToString();
                        serinity_online = content;
                        richTextBox1.AppendText("国服在线人数:" + serinity_online + "\n");
                    }
                    else
                    {
                        richTextBox1.AppendText("国服服务器不在线");
                    }
                }
                catch (Exception)
                {
                    richTextBox1.AppendText("连接国服服务器出错");
                }
                try
                {
                    TcpClient tranquility = new TcpClient("87.237.34.200", 26000);
                    if (tranquility.Connected == true)
                    {
                        byte[] data = new byte[1024];
                        NetworkStream ns = tranquility.GetStream();
                        ns.Read(data, 0, 1024);
                        string content = (data[21] * 256 + data[20]).ToString();
                        tranquility_online = content;
                        richTextBox1.AppendText("世界服在线人数:" + tranquility_online + "\n");
                    }
                    else
                    {
                        richTextBox1.AppendText("世界服服务器不在线");
                    }
                }
                catch (Exception k)
                {
                    MessageBox.Show(k.Message);
                    richTextBox1.AppendText("连接世界服服务器出错");
                }

            }
            catch (Exception k)
            {
                MessageBox.Show("error"+k);
            }

        }

        private void 代理人查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 检查与市场中心服务器连接ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ping checker = new Ping();
            PingReply reply = null;
            try
            {
                reply = checker.Send("www.ceve-market.org", 1000);
            }
            catch (Exception)
            {

            }
            finally
            {
                if (reply == null || (reply != null && reply.Status != IPStatus.Success))
                {
                    MessageBox.Show("连接国服市场中心失败，请检查网络连接或国服市场中心公告");
                }
                else if (reply.Status == IPStatus.Success)
                {
                    MessageBox.Show("连接正常");
                }
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void 标准代理人查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Artemis.Agents stdagt = new Artemis.Agents(Switch,"2");
            stdagt.Show();

        }

        private void 科研代理人查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Artemis.Agents resagt = new Artemis.Agents(Switch, "4");
            resagt.Show();
        }

        private void 星际导航系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Artemis.navigation nav = new Artemis.navigation(connectionstring);
            //nav.Show();
            //MessageBox.Show("该插件提供的为最短路径！无安等限制！请多加小心！");
            MessageBox.Show("Currently out of service");
        }

        private void 星系查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Artemis.Systems sys = new Artemis.Systems(connectionstring);
            sys.Show();
        }


       
    }
}
